package com.hc.server.actions;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import com.hc.server.Error;
import com.hc.server.IAction;
import com.hc.server.Request;
import com.hc.server.Response;
import com.hc.server.conn.mall.MallResponse;
import com.hc.server.conn.mall.MallServerHelper;

/**
 * 获取活动信息
 * @author xujh
 *
 */
public class QUERY_ACT implements IAction {

	private static final String SN = "SN";
	private static final String NAME = "NAME";
	private static final String PIC = "PIC";//图片地址
	private static final String ADD = "ADD";//地址
	private static final String USAGE = "USAGE";//使用说明
	private final static String STOCK = "STOCK";//库存
	private final static String SOLD = "SOLD";//已经销售
	private static final String DES = "DES";//描述
	private final static String MPRICE = "MPRICE";//市场价格
	private static final String SPRICE = "SPRICE";//销售价格
	
	private final static String MALL_NAME = "name";
	private final static String MALL_PIC = "picUri";
	private final static String MALL_SN = "pdtSn";
	private final static String MALL_ADD = "address";
	private final static String MALL_USAGE = "useExplain";
	private static final String MALL_STOCK = "stockCount";
	private static final String MALL_SOLD = "saleCount";
	private static final String MAll_MARKETPRICE = "marketPrice";
	private static final String MALL_SALEPRICE = "localPrice";
	private static final String MALL_DES = "brief";
	
	@Override
	public void execute(Request req, Response resp) {
		String pdtSn;
		try {
			pdtSn = req.getStrArg(SN);
		} catch (JSONException e) {
			resp.setErrResp(Error.INVALID_ARGS_ERR_CD, Error.INVALID_ARGS_ERR_MSG);
			return;
		}
		//到商城查询
		MallResponse mResp = null;
		try{
			mResp = MallServerHelper.queryActivity(pdtSn);
		}catch (IOException e) {
			e.printStackTrace();
			resp.setErrResp(Error.MALLSERVER_NET_ERR_CD, Error.MALLSERVER_NET_ERR_MSG);
			return;
		} catch (JSONException e) {
			e.printStackTrace();
			resp.setErrResp(Error.MALLSERVER_DATA_FORMAT_ERR_CD, Error.MALLSERVER_DATA_FORMAT_ERR_MSG);
			return;
		}
		if(0 != mResp.getRetCode()){
			resp.setErrResp(Error.MALLSERVER_ERR_CD, mResp.getErrorMsg());
			return;
		}
		
		try{
			JSONObject result = mResp.getResult();
			JSONObject tmp = result;//result.getJSONObject("detail");
					
			resp.appendRespContent(SN, tmp.getString(MALL_SN));
			resp.appendRespContent(NAME, tmp.getString(MALL_NAME));
			resp.appendRespContent(PIC, tmp.getString(MALL_PIC));
			resp.appendRespContent(ADD, tmp.getString(MALL_ADD));
			resp.appendRespContent(USAGE, tmp.getString(MALL_USAGE));
			resp.appendRespContent(STOCK, tmp.getInt(MALL_STOCK));
			resp.appendRespContent(DES, tmp.getString(MALL_DES));
			resp.appendRespContent(SOLD, tmp.getInt(MALL_SOLD));
			resp.appendRespContent(MPRICE, tmp.getInt(MAll_MARKETPRICE));
			resp.appendRespContent(SPRICE, tmp.getInt(MALL_SALEPRICE));			
		}catch (JSONException e) {
			e.printStackTrace();
			resp.setErrResp(Error.SERVER_SYS_ERR_CD, Error.SERVER_SYS_ERR_MSG);
			return;
		}

	}

}
